
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const vueplugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    entry: {
        app: ['babel-polyfill', './src/index.js'],
        cha: ['vuex', 'better-scroll', 'element-ui'],
    },
    output: {
        filename: 'js/[name].[hash:5].js',
        path: path.resolve(__dirname, './build'),
    },
    module: {
        rules: [
            {
                test: /\.js?$/,
                // exclude: /node_modules/,
                include: [path.resolve(__dirname, 'src')],
                exclude: [path.resolve(__dirname, 'node_modules')]
            },
            {
                test: /\.(gif|png|jpe?g|svg)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8*1024
                        }
                    }, {
                        loader: 'image-webpack-loader',
                        options: {
                            mozjpeg: {
                                progressive: true,
                                quality: 75
                            },
                            optipng: {
                                enabled: true
                            },
                            pngquant: {
                                quality: '65-90',
                                speed: 4
                            },
                            gifsicle: {
                                interlaced: false
                            }
                        }
                    }
                ]
            },
            {
                test: /\.(le|c)ss$/,
                use: [
                    MiniCssExtractPlugin.loader,'css-loader',{
                        loader: 'postcss-loader',
                        options: {
                            plugins: [require("autoprefixer")],
                        }
                    },'less-loader'
                ]
            },
            // {
            //     test: /\.css$/,
            //     use: [ 'style-loader', 'css-loader']
            // },
            // {
            //     test: /\.less$/,
            //     use: ['style-loader','css-loader', 'less-loader']
            // },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'fonts/[name]-[hash:5].[ext]',
                }
            },
            {
                test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 4096,
                            name: 'media/[name]-[hash:5].[ext]',
                        }
                    }
                ]
            },
            {
                test: /\.vue$/,
                loader: "vue-loader"
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.json', '.vue'],
        alias: {
            'src': path.resolve(__dirname, '../src'),
            'assets': path.resolve(__dirname, '../src/assets'),
            'components': path.resolve(__dirname, '../src/components')
        }
    },
    
    devServer: {
        contentBase: "./build",
        open: true,
        port: 5000,
        hot: true
    },
    optimization: {
        runtimeChunk: true,
        splitChunks: {
          chunks: "all"
        }
      },
    plugins: [
        new vueplugin(),
        new HtmlWebpackPlugin({
            template: './public/index.html',
            titel: 'react app',
            filename: 'index.html',
            chunks: ['app', 'cha']
        }),
        new MiniCssExtractPlugin({
            filename: "[name].[contenthash].css"
        })
    ],
}